const getMeau = () =>
  ajax({
    url: "http://chst.vip:1234/api/getindexmenu",
  });

getMeau().then((res) => {
  let arr = res.result;
  let firstArr = arr.slice(0, 8);
  //   console.log(firstArr);
  let seau = document.querySelector(".meau");
  let dv = seau.children;
  mapMeau(firstArr);
  let flag = true;

  dv[7].onclick = function () {
    if (flag) {
      mapMeau(arr);
      flag = !flag;
    } else {
      mapMeau(firstArr);
      flag = !flag;
    }
  };

  function mapMeau(Arr) {
    //渲染页面
    let html = "";
    for (let i = 0; i < Arr.length; i++) {
      let imgAdress = Arr[i].img;
      imgAdress = imgAdress.substring(10, imgAdress.length);
      html += `
    <div>
    <img src="../${imgAdress}
    <p>${Arr[i].name}</p>
    </div>`;
    }
    seau.innerHTML = html;

    //扩展栏收放
    for (let i = 0; i < dv.length; i++) {
      dv[i].onclick = function () {
        if (i != 7) {
          //   console.log(arr[i].titlehref);
          location.href = arr[i].titlehref;
        } else {
          if (flag) {
            mapMeau(arr);
            flag = !flag;
          } else {
            mapMeau(firstArr);
            flag = !flag;
          }
        }
      };
    }
  }
});

// 请求首页的折扣列表
const getProuct = () =>
  ajax({
    url: "http://chst.vip:1234/api/getmoneyctrl",
  });

getProuct().then((res) => {
  let productUl = document.querySelector('.product>ul')
  let arr = res.result
  // console.log(arr);
  let html = ''
    for(let i = 0 ; i< arr.length ; i++){
        html += ` <li productid=${arr[i].productId}>
        ${arr[i].productImgLg || arr[i].productImgSm ||arr[i].productImg2}
        <div>
            <p>${arr[i].productName}<span>${arr[i].productPinkage}</span></p>
            <p>${arr[i].productFrom} | ${arr[i].productTime}<span>${arr[i].productComCount}</span></p>
        </div>
        </li>`
    }
    productUl.innerHTML = html

    let productLi = productUl.children
    for(let i = 0 ; i< productLi.length ; i++){
        productLi[i].onclick =function(){
            console.log(this.getAttribute("productid"));
            localStorage.setItem('proId', this.getAttribute("productid"));
            location.href = "discount.html";
        }
    }
});


// 点击回到顶部
let backTop = document.querySelector('.footer-main>div>a')
backTop.onclick = function(e){
    e.preventDefault()    
    let distans = document.documentElement.scrollTop
    let distansControl = setInterval(function(){
      distans -= 10
      document.documentElement.scrollTop = distans
      if(distans <= 0){
        clearInterval(distansControl)
      }
    },3)  
}
